//
//  CKCycleView.m
//  CycleScrollViewDemo
//
//  Created by lanou3g on 16/4/19.
//  Copyright © 2016年 李日青. All rights reserved.
//

#import "CKCycleView.h"

@interface CKCycleView ()<UIScrollViewDelegate>
//scrollerView
@property (nonatomic,strong)UIScrollView *imgScrollView;
@property (nonatomic,strong)UIPageControl *imgpageController;
//存放图片的资源？
@property (nonatomic,strong)NSMutableArray *imgViewArray;
//存放图片数组
@property (nonatomic,strong)NSMutableArray *imgArray;
//展示当前的图片下标
@property (nonatomic, assign)NSInteger index;
//定时机器
@property (nonatomic,strong)NSTimer *imgTimer;
//点击回调Block
@property (nonatomic,copy)void(^tapBlock)(NSInteger index);
//时间间隔
@property (nonatomic,assign)NSTimeInterval interval;
@end

@implementation CKCycleView

//初始化方法
- (instancetype)initWithFrame:(CGRect)frame interVal:(NSTimeInterval)interval {
    self = [super initWithFrame:frame];
    if (self) {
        self.imgViewArray = [NSMutableArray new];
        self.imgArray = [NSMutableArray new];
        self.imgScrollView = [[UIScrollView alloc]initWithFrame:self.bounds];
        self.imgScrollView.contentSize = CGSizeMake(frame.size.width * 3, frame.size.height);
        [self addSubview:self.imgScrollView];
        for (int i = 0; i < 3; i++) {
            UIImageView *imgView = [[UIImageView alloc]initWithFrame:CGRectMake(frame.size.width *i, 0, frame.size.width, frame.size.height)];
            [self.imgScrollView addSubview:imgView];
            [self.imgViewArray addObject:imgView];
            self.interval = interval;
            self.imgScrollView.pagingEnabled = YES;
            self.imgScrollView.showsVerticalScrollIndicator = NO;
            self.imgScrollView.delegate = self;
            

        }
       
    }
    return self;
}
//图片滚动方法
- (void)timerAction {
    [self.imgScrollView setContentOffset:CGPointMake(self.imgScrollView.frame.size.width * 2, 0)animated:YES];
    //改变index
    self.index = (self.index + 1) % self.imgNameArray.count;
//    [self layoutimages];

}
//滚动动画结束
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView {
    //回原来位置
    [self.imgScrollView setContentOffset:CGPointMake(scrollView.frame.size.width, 0)animated:NO];
    [self layoutimages];
}


//图片名数组setter
- (void)setImgNameArray:(NSMutableArray *)imgNameArray {
    if (_imgNameArray != imgNameArray) {
        _imgNameArray = imgNameArray;
//        [self.imgpageController removeFromSuperview];
        self.imgpageController = [[UIPageControl alloc]initWithFrame:CGRectMake(0, self.frame.size.height - 40, self.frame.size.width, 40)];
        [self addSubview:self.imgpageController];
        self.imgpageController.currentPage = 0;
        self.imgpageController.numberOfPages = imgNameArray.count;
        self.imgpageController.enabled = NO;//疑点！！！！！！
               for (int i = 0; i <  imgNameArray.count; i++) {
            UIImage *img = [UIImage imageNamed:imgNameArray[i]];
                   [self.imgArray addObject:img];
            if (i < 3 ) {
                [self.imgViewArray[i] setImage:img];
            }
        }
        //设置当前偏移量
        [self.imgScrollView setContentOffset:CGPointMake(self.frame.size.width, 0)animated:NO];
        self.index = 0;
        [self layoutimages];
        //点击手势
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapCycleViewAction:)];
        [(UIImageView *)self.imgViewArray[1]addGestureRecognizer:tap];
        [(UIImageView *)self.imgViewArray[1]setUserInteractionEnabled:YES];
        //初始化timer
        if (self.interval <= 0) {
            self.imgTimer = [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(timerAction) userInfo:nil repeats:YES];
        }else {
            self.imgTimer = [NSTimer scheduledTimerWithTimeInterval:self.interval target:self selector:@selector(timerAction) userInfo:nil repeats:YES];
        }

    }
}


- (void)tapCycleViewAction:(UITapGestureRecognizer *)tap {
    if (self.tapBlock) {
        self.tapBlock(self.index);
    }
}



#pragma mark - 代理方法


static CGFloat x = 0;
//开始拖动
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    x = scrollView.contentOffset.x;
    [self.imgTimer setFireDate:[NSDate distantFuture]]; 
}
//减速结束动画
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    if (x > scrollView.contentOffset.x) {
        [self panToleft:NO Index:self.index];
    }else if(x < scrollView.contentOffset.x){
        [self panToleft:YES Index:self.index];
    }
    [self.imgTimer setFireDate:[NSDate dateWithTimeIntervalSinceNow:2]];
}

#pragma 向左滑动向右滑动结束切换
- (void)panToleft:(BOOL)left Index:(NSInteger)index {
    
    if (!left) {
        self.index = (index - 1 + self.imgNameArray.count) % self.imgNameArray.count;
    }else {
        self.index = (index + 1) % self.imgNameArray.count;
    }
    //切换图片
    [self layoutimages];
   }

- (void)layoutimages{
    for (int i = 0 ; i < 3; i++) {
        [self.imgViewArray[i] setImage:self.imgArray[(self.index - 1 + i + self.imgNameArray.count) % self.imgNameArray.count]];
        //回位操作
        [self.imgScrollView setContentOffset:CGPointMake(self.frame.size.width, 0)animated:NO];

    }
}

- (void)setIndex:(NSInteger)index {
    if (_index != index) {
        _index = index;
        self.imgpageController.currentPage = index;
    }
}

//添加Block回调
- (void)addTapBlock:(void(^)(NSInteger index))block{

    self.tapBlock = block;
}











@end
